package com.aplink.generic.util;

import android.util.Log;

import com.aplink.dellcommercial.BuildConfig;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class LogUtil {
    public static final String TAG = "DellCommercial";
    private final String mClassName;

    public LogUtil( Class<?> object ) {
        this.mClassName = object.getSimpleName();
    }

    public LogUtil( String className ) {
        this.mClassName = className;
    }

    private String formatError( String message, String action ) {
        final SimpleDateFormat sdf = new SimpleDateFormat(
                "dd.MM.yyyy HH:mm:ss", Locale.getDefault() );
        final String currentDateandTime = sdf.format( new Date() );

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put( "Class:", mClassName );
            jsonObject.put( "Datetime:", currentDateandTime );
            jsonObject.put( "Message:", message );
            return jsonObject.toString( 4 ).replace( "\\", "" );
        } catch ( JSONException e ) {
            e.printStackTrace();
        }
        return null;
    }

    private String formatError( String message, String detailMessage,
                                String action ) {
        final SimpleDateFormat sdf = new SimpleDateFormat(
                "dd.MM.yyyy HH:mm:ss", Locale.getDefault() );
        final String currentDateandTime = sdf.format( new Date() );

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put( "Class:", mClassName );
            jsonObject.put( "Datetime:", currentDateandTime );
            jsonObject.put( "Message:", message );
            jsonObject.put( "Error Message:", detailMessage );
            return jsonObject.toString( 4 ).replace( "\\", "" );
        } catch ( JSONException e ) {
            e.printStackTrace();
        }
        return null;
    }

    public void error( String message, String action, Exception ex ) {
        if ( BuildConfig.DEBUG ) {
            Log.e( TAG, this.formatError( message, ex.toString(), action ) );

        }
    }

    public void info( String message, String action ) {
        if ( BuildConfig.DEBUG ) {
            Log.i( TAG, this.formatError( message, action ) );
        }
    }
}
